package com.esm.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.esm.model.Attend;
import com.esm.service.SelectRecordService;
import com.esm.serviceimpl.SelectRecordServiceImpl;

import net.sf.json.JSONObject;

/**
 * 条件查询出差记录 Controller
 * @author 康少
 *
 * 2017年1月7日
 */
public class SeleBtravelByTermController extends HttpServlet{
	SelectRecordService select = new SelectRecordServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String dep_name = req.getParameter("dep_name");
		String y = req.getParameter("year");
		String m = req.getParameter("month");
		int page = Integer.parseInt(req.getParameter("page"));
		int rows = Integer.parseInt(req.getParameter("rows"));
		if (y != null ) {
			int year = Integer.parseInt(y);
			int month = Integer.parseInt(m);
			int count = select.getBtravelCountByTerm(dep_name, year, month);
			List<Attend> attends = select.getBtravelAttendByTerm(dep_name, year, month, page, rows);
			JSONObject jo = new JSONObject();
			jo.put("total", count);
			jo.put("rows", attends);
			PrintWriter pw = resp.getWriter();
			pw.write(jo.toString());
			pw.flush();
			pw.close();
		} 
	}
}
